/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.util.math;

/**
 * Helper class providing bit-shift functions and Primitive<->byte array
 * conversions
 */
public final class BitUtil {

	public static char byteToChar(byte[] bytes, int offset) {
		int high = bytes[offset] & 0xff;
		int low = bytes[offset + 1] & 0xff;
		return (char) ((int) (high << 8 | low));
	}

	public static int byteToInt(byte[] bytes, int offset) {
		int value = 0;
		for (int i = 0; i < 4; i++) {
			int shift = (4 - 1 - i) * 8;
			value += (bytes[i + offset] & 0x000000FF) << shift;
		}
		return value;
	}

	public static byte[] charToByte(final char value) {
		byte[] charBuffer = new byte[2];
		charBuffer[0] = (byte) ((value >> 8) & 0x000000FF);
		charBuffer[1] = (byte) (value & 0x00FF);
		return charBuffer;
	}

	/**
	 * Returns the number of contiguous 0 bits starting with the most
	 * significant bit of x.
	 */
	public static int countLeadingZeros(int x) {
		if (x <= 0) {
			if (x == 0) {
				return 32;
			}
			return 0;
		}
		int count = 0;
		if ((x & 0xffff0000) == 0) {
			x <<= 16;
			count = 16;
		}
		if ((x & 0xff000000) == 0) {
			x <<= 8;
			count += 8;
		}
		while (x > 0) {
			count++;
			x <<= 1;
		}
		return count;
	}

	/**
	 * Returns the number of contiguous 0 bits starting with the most
	 * significant bit of x.
	 */
	public static int countLeadingZeros(long x) {
		int c = countLeadingZeros((int) (x >> 32));
		if (c == 32) {
			return countLeadingZeros((int) x) + 32;
		}
		return c;
	}

	public static byte[] intToByte(final int value) {
		byte[] integerBuffer = new byte[4];
		integerBuffer[0] = (byte) ((value >> 24) & 0x000000FF);
		integerBuffer[1] = (byte) ((value >> 16) & 0x000000FF);
		integerBuffer[2] = (byte) ((value >> 8) & 0x000000FF);
		integerBuffer[3] = (byte) (value & 0x00FF);
		return integerBuffer;
	}

	/**
	 * Right-shift x by count bits, and round the result using half-even
	 * rounding.
	 */
	public static int roundingRightShift(int x, int count) {
		int remainder;
		if (count > 32) {
			return 0;
		} else if (count == 32) {
			remainder = x;
			x = 0;
		} else {
			remainder = x << (32 - count);
			x >>>= count;
		}
		if ((remainder < 0) && ((remainder != 0x80000000) || ((x & 1) == 1))) {
			return x + 1;
		}
		return x;
	}

	/**
	 * Right-shift x by count bits, and round the result using half-even
	 * rounding.
	 */
	public static long roundingRightShift(long x, int count) {
		long remainder;
		if (count > 64) {
			return 0;
		} else if (count == 64) {
			remainder = x;
			x = 0;
		} else {
			remainder = x << (64 - count);
			x >>>= count;
		}
		if ((remainder < 0) && ((remainder != 0x8000000000000000L) || ((x & 1) == 1))) {
			return x + 1;
		}
		return x;
	}

	/**
	 * Right-shift x by count bits, and if any of the shifted-off bits are 1,
	 * set the least significant bit of the return value to 1.
	 */
	public static int stickyRightShift(int x, int count) {
		if (count >= 32) {
			return ((x == 0) ? 0 : 1);
		} else if ((x << (32 - count)) == 0) {
			return x >>> count;
		} else {
			return (x >>> count) | 1;
		}
	}

	/**
	 * Right-shift x by count bits, and if any of the shifted-off bits are 1,
	 * set the least significant bit of the return value to 1.
	 */
	public static long stickyRightShift(long x, int count) {
		if (count >= 64) {
			return ((x == 0) ? 0 : 1);
		} else if ((x << (64 - count)) == 0) {
			return x >>> count;
		} else {
			return (x >>> count) | 1;
		}
	}

	private BitUtil() {}
}
